package com.xx.java8.utils;


import com.xx.java8.chapter2.AppleFormatter;
import com.xx.java8.domain.Apple;
import com.xx.java8.enums.ColorEnum;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;

public class CollectionUtils {
    @org.jetbrains.annotations.NotNull
    public static List<Apple> initAppleList() {
        List<Apple> appleList;
        appleList = new ArrayList<Apple>();
        for (int i = 0; i < 10; i++) {
            Apple apple = new Apple();
            apple.setWeight(Math.random() * 53);
            apple.setColor(i % 2 == 0 ? ColorEnum.GREEN : ColorEnum.RED);
            appleList.add(apple);
        }
        return appleList;
    }

    public static <T> Collection<T> filter(Collection<T> collection, Predicate<T> predicate) {
        Collection<T> result = new ArrayList<>();
        for (T t : collection) {
            if (predicate.test(t)) {
                result.add(t);
            }
        }
        return result;
    }

    public static void appleListFormatted(List<Apple> appleList, AppleFormatter formatter) {
        for (Apple apple : appleList) {
            System.out.println(formatter.accept(apple));
        }
    }
}
